package com.wish.group05.shoufang.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.alibaba.nacos.common.utils.UuidUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itextpdf.io.codec.Base64;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
import com.wish.group05.base.dto.HouseInfoDto;
import com.wish.group05.base.entity.*;
import com.wish.group05.base.service.*;
import com.wish.group05.base.vo.HouseInfoVO;
import com.wish.group05.common.core.utils.StringUtils;
import com.wish.group05.shoufang.sign.*;
import com.wish.group05.shoufang.util.PDFUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wish.group05.common.log.annotation.Log;
import com.wish.group05.common.log.enums.BusinessType;
import com.wish.group05.common.security.annotation.PreAuthorize;

import com.wish.group05.common.core.web.controller.BaseController;
import com.wish.group05.common.core.web.domain.AjaxResult;
import com.wish.group05.common.core.utils.poi.ExcelUtil;
import com.wish.group05.common.core.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author wish
 * @date 2022-05-26
 */
@RestController
@RequestMapping("/contract")
public class TSfContractController extends BaseController
{
    @Autowired
    private ISfContractService iSfContractService;
    @Autowired
    ICwPaymentPlanService iCwPaymentPlanService;
    @Autowired
    private IHouseInfoService iHouseInfoService;
    @Autowired
    MarkService markService;



    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize(hasPermi = "shoufang:contract:list")
    @GetMapping("/list")
    public TableDataInfo list(SfContract tSfContract)
    {
        QueryWrapper<SfContract> queryWrapper = new QueryWrapper<>();
        startPage();
        //查询合同编号
        if(!StringUtils.isEmpty(tSfContract.getSfContractNo())){
            queryWrapper.like("sf_contract_no",tSfContract.getSfContractNo());
        }
        //查询小区名称
        if(!StringUtils.isEmpty(tSfContract.getPremiseName())){
            queryWrapper.like("premise_name",tSfContract.getPremiseName());
        }
        //查询业主姓名
        if(!StringUtils.isEmpty(tSfContract.getOwnerName())){
            queryWrapper.like("owner_name",tSfContract.getOwnerName());
        }
        //查询录入日期
        if(tSfContract.getConInfoTime()!=null){
            queryWrapper.gt("con_info_time",tSfContract.getConInfoTime());
        }
        List<SfContract> list = iSfContractService.list(queryWrapper);
        return getDataTable(list);
    }
    @PreAuthorize(hasPermi = "shoufang:contract:paylist")
    @GetMapping("/paylist")
    public TableDataInfo paylist(SfContract sfContract)
    {
        startPage();
        List<CwPaymentPlan> payments = iCwPaymentPlanService.getPayments(sfContract);
        return getDataTable(payments);
    }

    /**
     * 查询【查询房产信息】列表
     */
    @PreAuthorize(hasPermi = "shoufang:contract:houselist")
    @GetMapping("/houselist")
    public TableDataInfo houselist(HouseInfoDto houseInfoDto)
    {
        startPage();
        List<HouseInfoVO> houseInfoVOList = iHouseInfoService.selectTbHouseInfoList(houseInfoDto);
        TableDataInfo dataTable = getDataTable(houseInfoVOList);
        int size = houseInfoVOList.size();
        dataTable.setTotal(Long.parseLong(size+""));
        return dataTable;
    }


    /**
     * 获取
     * @param id
     * @return
     */
    @PreAuthorize(hasPermi = "shoufang:contract:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        SfContract sfContract = iSfContractService.getById(id);
        return AjaxResult.success(sfContract);
    }


    /**
     * 修改
     * @param
     * @return
     */
    @PreAuthorize(hasPermi = "shoufang:contract:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SfContract sfContract)
    {
        boolean b = iSfContractService.saveOrUpdate(sfContract);
        return toAjax(b==true?1:0);
    }
        //新增【请填写功能名称】
        @PreAuthorize(hasPermi = "shoufang:contract:add")
        @Log(title = "添加", businessType = BusinessType.INSERT)
        @PostMapping
        public AjaxResult add(@RequestBody SfContract sfContract)
        {
            Snowflake snowflake = IdUtil.createSnowflake(31, 31);
            long nextId = snowflake.nextId();
            String value = String.valueOf(nextId);
            sfContract.setSfContractNo(value);
            sfContract.setBusinessId(Long.valueOf("2324"));
            sfContract.getId();
            boolean save = iSfContractService.save(sfContract);
            return toAjax(save==true?1:0);
        }
    //删除【请填写功能名称】

        @PreAuthorize(hasPermi = "shoufang:contract:remove")
        @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
        @DeleteMapping("/{ids}")
        public AjaxResult remove(@PathVariable Long[] ids)
        {
            boolean b = iSfContractService.removeByIds(Arrays.asList(ids));
            return toAjax(b==true?1:0);
        }
        //生成pdf
        @GetMapping("getPdfAll/{id}")
        public AjaxResult getPdf(@PathVariable("id") String id) throws Exception {

            String filePath=DeferredSignPdfAssLryTest.FOLDER+"/"+DeferredSignPdfAssLryTest.uuid+"_my.pdf";
            Document document = new Document();
            //pdf输入流
            PdfWriter doc = PDFUtils.createDoc(document, filePath);
            SfContract contract = iSfContractService.getById(id);
            List<SfContract> contractList = iSfContractService.list(new QueryWrapper<SfContract>().eq("id", id));
            //添加表头
            ArrayList<String> list = new ArrayList<>();
            list.add("电子合同");
            list.add("签约人:"+contract.getSignUserName());
            list.add("签约日期"+contract.getConSignTime());
            PDFUtils.addContent(document,list,8);
            //添加表格
            PDFUtils.addTable(doc,new String[]{"id","合同编号","小区名称"},new String[]{"id","sfContractNo","premiseName"},contractList ,70,670);
            document.close();
            doc.close();
            //加水印
            markService.markPdf();
            return AjaxResult.success(DeferredSignPdfAssLryTest.uuid+"_empty.pdf");
        }

}
